package values.shop
{
import flash.events.EventDispatcher;

import utils.DateUtil;

import values.common.CostValue;
import values.events.ShopItemValueEvent;
import values.item.BuildingItemValue;
import values.item.EquipmentItemValue;
import values.item.IItemValue;
import values.item.ItemTemplateValueList;
import values.item.ItemValue;


[Event(name="totalRestNumChanged", type="values.events.ShopItemValueEvent")]
[Event(name="todayRestNumChanged", type="values.events.ShopItemValueEvent")]
/**
 * ShopItemValue
 * @author jliang
 *
 */
public class ShopItemValue extends EventDispatcher
{    
    [Data(type="field", mapping="@key")]
    public var key:String;
    
    [Data(type="field", mapping="@now_cost")]
    public var nowCostDef:String;
    
    protected var _nowCost:CostValue;

    public function get nowCost():CostValue
    {
        if (!_nowCost)
        {
            _nowCost = new CostValue();
            _nowCost.addItemValueObject(costItemKey, nowCostNum);
        }
        return _nowCost;
    }
    
    public function get costItemKey():String
    {
        return String(nowCostDef.split(":")[0]);
    }
    
    public function get nowCostNum():Number
    {
        return Number(nowCostDef.split(":")[1]);
    }
    
    [Data(type="field", mapping="@old_cost")]
    public var oldCostNum:Number;
    
    public function get isCashCost():Boolean
    {
        return nowCost.cashCost > 0;
    }
    
    public function get isInDiscount():Boolean
    {
        return !isNaN(oldCostNum) && oldCostNum > 0;
    }
    
    [Data(type="field", mapping="@count_limit")]
    public var countLimit:Number;
    
    [Data(type="field", mapping="@daily_limit")]
    public var dailyLimit:Number;
    
    public function get hasCountLimit():Boolean
    {
        return !isNaN(countLimit) && countLimit > 0;
    }
    
    public function get hasDailyLimit():Boolean
    {
        return !isNaN(dailyLimit) && dailyLimit > 0;
    }
    
    [Data(type="field", mapping="@time_on")]
    public var timeOnDef:String;
    
    protected var _timeOn:Number = -1;

    public function get timeOn():Number
    {
        if (_timeOn < 0)
        {
            _timeOn = hasTimeOn ? DateUtil.parseTimeStringToSec(timeOnDef) : 0;
        }
        return _timeOn;
    }
    
    public function get hasTimeOn():Boolean
    {
        return timeOnDef != null && timeOnDef != "";
    }
    
    [Data(type="field", mapping="@time_off")]
    public var timeOffDef:String;
    
    protected var _timeOff:Number = -1;
    
    public function get timeOff():Number
    {
        if (_timeOff < 0)
        {
            _timeOff = hasTimeOff ? DateUtil.parseTimeStringToSec(timeOffDef) : 0;
        }
        return _timeOff;
    }
    
    public function get hasTimeOff():Boolean
    {
        return timeOffDef != null && timeOffDef != "";
    }    
    
    [Data(type="field", mapping="def", method="json")]
    public var itemDef:Array;
    
    protected var _itemValue:IItemValue;

    public function get itemValue():IItemValue
    {
        if (!_itemValue)
        {
            _itemValue = ItemValue.create(ItemTemplateValueList.getByKey(itemDef[0]), null, itemDef[1]);
        }
        return _itemValue;
    }
    
    public function generateItems(count:int = 1):Vector.<IItemValue>
    {
        var items:Vector.<IItemValue> = new Vector.<IItemValue>();
        for (var i:int = 0; i < count; i++)
        {
            items.push(ItemValue.create(ItemTemplateValueList.getByKey(itemDef[0]), null, itemDef[1]));
        }
        return items;
    }
    
    public function get name():String
    {
        return itemValue.name;
    }
    
    public function get desc():String
    {
        return itemValue.desc;
    }
    
    public function get isBuildingItem():Boolean
    {
        return itemValue is BuildingItemValue;
    }
    
    public function get isEquipmentItem():Boolean
    {
        return itemValue is EquipmentItemValue;
    }
    
    private var _totalRestNum:Number = Number.MAX_VALUE;

    public function get totalRestNum():Number
    {
        return _totalRestNum;
    }

    public function set totalRestNum(value:Number):void
    {
        _totalRestNum = value;
        
        this.dispatchEvent(new ShopItemValueEvent(ShopItemValueEvent.TOTAL_REST_NUM_CHANGED));
    }
    
    private var _todayRestNum:Number = Number.MAX_VALUE;

    public function get todayRestNum():Number
    {
        return _todayRestNum;
    }

    public function set todayRestNum(value:Number):void
    {
        _todayRestNum = value;
        
        this.dispatchEvent(new ShopItemValueEvent(ShopItemValueEvent.TODAY_REST_NUM_CHANGED));
    }
}
}






